package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.entity.MSamplingTask;
import com.lsyw.intergrate.service.MSamplingTaskService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.MSamplingTaskProblemService;
import com.lsyw.intergrate.entity.MSamplingTaskProblem;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 手动采样任务问题表 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-01-19
 */
@Api(tags = "手动采样任务问题表")
@RestController
@RequestMapping("/m-sampling-task-problem")
public class MSamplingTaskProblemController {


    @Autowired
    private MSamplingTaskProblemService mSamplingTaskProblemService;
    @Autowired
    private MSamplingTaskService mSamplingTaskService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(mSamplingTaskProblemService.getById(id));
    }
    /**
     * 通过id查询
     */
    @GetMapping("/updateStatus")
    public Result updateStatus(String id) {
        MSamplingTaskProblem mSamplingTaskProblem = mSamplingTaskProblemService.getById(id);
        mSamplingTaskProblem.setStatus(2);
        mSamplingTaskProblem.setGmtModified(new Date());
        mSamplingTaskProblemService.updateById(mSamplingTaskProblem);
        return Result.successResult("修改成功");
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody MSamplingTaskProblem mSamplingTaskProblem) {
        mSamplingTaskProblem.setStatus(1);
        mSamplingTaskProblem.setDelFlag(0);
        mSamplingTaskProblem.setGmtCreate(new Date());
        mSamplingTaskProblemService.save(mSamplingTaskProblem);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        mSamplingTaskProblemService.removeById(Long.parseLong(id));
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody MSamplingTaskProblem mSamplingTaskProblem) {
        mSamplingTaskProblem.setGmtModified(new Date());
        mSamplingTaskProblemService.updateById(mSamplingTaskProblem);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody MSamplingTaskProblemReqVo mSamplingTaskProblem) {
        final LambdaQueryWrapper<MSamplingTaskProblem> lambda = new QueryWrapper<MSamplingTaskProblem>().lambda();
        this.buildCondition(lambda, mSamplingTaskProblem);
        return Result.successResult(mSamplingTaskProblemService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MSamplingTaskProblem> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MSamplingTaskProblem> lambda = new QueryWrapper<MSamplingTaskProblem>().lambda();
        lambda.eq(MSamplingTaskProblem::getDelFlag,0);
        lambda.eq(MSamplingTaskProblem::getMSamplingTaskId,pageInfo.getCondition().get("mSamplingTaskId").toString());
        if (pageInfo.getCondition().get("name") != null && !StringUtils.isEmpty(pageInfo.getCondition().get("name").toString())) {
            lambda.like(MSamplingTaskProblem::getName, pageInfo.getCondition().get("name"));
        }
        IPage<MSamplingTaskProblem> iPage = this.mSamplingTaskProblemService.page(page, lambda);
        iPage.getRecords().forEach(e->{
            MSamplingTask mSamplingTask =mSamplingTaskService.getById(e.getMSamplingTaskId());
            e.setMSamplingTaskName(mSamplingTask.getName());
        });
        return Result.successResult(iPage);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MSamplingTaskProblem> lambda, MSamplingTaskProblemReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MSamplingTaskProblem::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getMSamplingTaskId())) {
            lambda.eq(MSamplingTaskProblem::getMSamplingTaskId, param.getMSamplingTaskId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(MSamplingTaskProblem::getName, param.getName());
        }
        if (!CollectionUtils.isEmpty(param.getProblemDateList())) {
            lambda.ge(MSamplingTaskProblem::getProblemDate, param.getProblemDateList().get(0));
            lambda.le(MSamplingTaskProblem::getProblemDate, param.getProblemDateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(MSamplingTaskProblem::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getContent())) {
            lambda.eq(MSamplingTaskProblem::getContent, param.getContent());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MSamplingTaskProblem::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MSamplingTaskProblem::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MSamplingTaskProblem::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MSamplingTaskProblem::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MSamplingTaskProblem::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MSamplingTaskProblem::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(MSamplingTaskProblem::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, MSamplingTaskProblem::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MSamplingTaskProblemReqVo extends MSamplingTaskProblem {
        private List<String> problemDateList; // 问题时间起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
